package com.paas.form.controller;


import com.alibaba.fastjson.JSON;
import com.bpm.feign.model.ProcessCmd;
import com.bpm.feign.service.IBpmProcessInstanceService;
import com.paas.common.model.BaseUserInfo;
import com.paas.common.userAgent.Utils;
import com.paas.common.util.StringUtil;
import com.paas.common.util.UniqueIdUtil;
import com.paas.common.web.SystemConst;
import com.paas.form.entity.FormDefinitionEntity;
import com.paas.form.entity.FormViewResourceEntity;
import com.paas.form.service.IFormDefinitionService;
import com.paas.form.service.IFormViewResourceService;
import com.paas.form.vo.FormResManuVo;
import com.publics.entity.UcResourcesEntity;
import com.publics.feign.service.IUcResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paas.common.util.BeanUtil;
import com.paas.common.model.RestResponse;
import com.paas.common.web.annotion.OperLog;
import com.paas.common.web.controller.BaseController;
import com.paas.common.web.oper.OprLogConstant;


import com.paas.form.entity.FormManuRecordsEntity;
import com.paas.form.service.IFormManuRecordsService;

@RestController
@RequestMapping(value = "/form/formManuRecords")
@Api(description = "菜单上下架记录表", tags = "")
public class FormManuRecordsController extends BaseController {

    @Autowired
    public IFormManuRecordsService formManuRecordsService;

    @Autowired
    public IBpmProcessInstanceService bpmProcessInstanceService;

    @Autowired
    public IFormDefinitionService formDefinitionService;

    @Autowired
    public IFormViewResourceService formViewResourceService;

    @Autowired
    public IUcResourcesService ucResourcesService;


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @OperLog(operType = OprLogConstant.ADD,operModul = "",operDescribe = "-保存")
    public void save(@RequestBody FormManuRecordsEntity formManuRecordsEntity) throws ParseException{
		if(BeanUtil.isNotEmpty(formManuRecordsEntity)){
			injectUpdateDataAuth(formManuRecordsEntity);
		}
    	formManuRecordsService.save(formManuRecordsEntity);
    }

    @RequestMapping(value = "/saveManuReocords",method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    public RestResponse saveManuReocords(@RequestBody FormResManuVo<ProcessCmd> formResManuVo) throws ParseException{
        if(BeanUtil.isNotEmpty(formResManuVo)){
            BaseUserInfo baseUserInfo = getUserInfo();
            FormManuRecordsEntity formManuRecordsEntity = formResManuVo.getFormManuRecordsEntity();
            if (BeanUtil.isNotEmpty(formManuRecordsEntity)){
                if ("1".equals(formManuRecordsEntity.getType())){
                    formManuRecordsEntity.setAppId(baseUserInfo.getAppId());
                    formManuRecordsEntity.setCreateId(baseUserInfo.getUserId());
                    formManuRecordsEntity.setOrgId(baseUserInfo.getOrgId());
                    formManuRecordsService.insert(formManuRecordsEntity);
                    FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(formManuRecordsEntity.getLinkId());
                    formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUTONING);
                    formDefinitionService.updateById(formDefinitionEntity);
                }else {
                    formManuRecordsEntity.setAppId(baseUserInfo.getAppId());
                    formManuRecordsEntity.setCreateId(baseUserInfo.getUserId());
                    formManuRecordsEntity.setOrgId(baseUserInfo.getOrgId());
                    formManuRecordsService.insert(formManuRecordsEntity);
                    FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(formManuRecordsEntity.getLinkId());
                    formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUTOFFING);
                    formDefinitionService.updateById(formDefinitionEntity);
                }

            }


            ProcessCmd processCmd = formResManuVo.getProcessCmd();
            if(BeanUtil.isNotEmpty(processCmd)){
                processCmd.setBusinessKey(formManuRecordsEntity.getLinkId());
                processCmd.setData(JSON.toJSONString(formManuRecordsEntity));
                processCmd.setAppId(baseUserInfo.getAppId());
                bpmProcessInstanceService.startProcessInstance(processCmd);
            }


        }
        return RestResponse.success();
    }

    @RequestMapping(value = "/saveBatch",method = RequestMethod.POST)
    @ApiOperation(value = "批量保存信息", notes = "批量保存信息")
    @OperLog(operType = OprLogConstant.UPDATE,operModul = "",operDescribe = "-批量保存修改")
    public void saveBatch(@RequestBody List<FormManuRecordsEntity> list) throws ParseException{
		if(list!=null && list.size()>0){
            injectUpdateDataAuth(list);
        }
    	formManuRecordsService.saveBatch(list);
    }

    @RequestMapping(value = "/delectById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id删除数据", notes = "根据Id删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-删除")
    public void delectById(String id){
		if (null != id && !"".equals(id)) {
			FormManuRecordsEntity formManuRecordsEntity = new FormManuRecordsEntity();
			injectUpdateDataAuth(formManuRecordsEntity);
			formManuRecordsEntity.setId(id);
			formManuRecordsService.deleteByEntity(formManuRecordsEntity);
		}
    }

    @RequestMapping(value = "/deleteBatchByIds",method = RequestMethod.POST)
    @ApiOperation(value = "根据Id批量删除数据", notes = "根据Id批量删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-批量删除")
    public void deleteBatchByIds(@RequestBody List<String> ids){
		if (null != ids && ids.size() > 0) {
			FormManuRecordsEntity formManuRecordsEntity = new FormManuRecordsEntity();
			injectUpdateDataAuth(formManuRecordsEntity);
			//formManuRecordsService.deleteBatchIds(ids);
			formManuRecordsService.deleteBatchIdsWithPermission(ids,formManuRecordsEntity);
		}
    }

    @RequestMapping(value = "/queryById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id查询对象", notes = "根据Id查询对象")
    public FormManuRecordsEntity queryById(String id){
         FormManuRecordsEntity bean = formManuRecordsService.getById(id);
         return bean;
    }

    @RequestMapping(value = "/queryBatchByIds",method = RequestMethod.POST)
    @ApiOperation(value = "根据Id查询对象列表", notes = "根据Id查询对象列表")
    public List<FormManuRecordsEntity> queryBatchByIds(@RequestBody List<String> ids){
         List<FormManuRecordsEntity> list = formManuRecordsService.getBatchByIds(ids);
         return list ;
    }

    @RequestMapping(value = "/queryPage",method = RequestMethod.GET)
    @ApiOperation(value = "模板分页查询", notes = "模板分页查询")
    public IPage<FormManuRecordsEntity> queryPage(FormManuRecordsEntity formManuRecordsEntity, Integer currentPage, Integer pageSize){
    	if (currentPage == null) {
    		currentPage = 1;
    	}
    	if (pageSize == null) {
    		pageSize = 10;
    	}
		injectCheckDataAuth(formManuRecordsEntity);
    	IPage<FormManuRecordsEntity> pageBean = formManuRecordsService.getPage(formManuRecordsEntity, currentPage, pageSize);
    	return pageBean;
    }

    @RequestMapping(value = "/queryList",method = RequestMethod.GET)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    public List<FormManuRecordsEntity> queryList(FormManuRecordsEntity formManuRecordsEntity){
		injectCheckDataAuth(formManuRecordsEntity);
    	List<FormManuRecordsEntity> list = formManuRecordsService.getList(formManuRecordsEntity);
        return list;
    }

    @RequestMapping(value = "/agreePutOnApproval", method = RequestMethod.POST)
    @ApiOperation(value = "上架申请通过", notes = "上架申请通过")
    public RestResponse agreePutOnApproval(String bussinessKey) {
        if (StringUtil.isNotEmpty(bussinessKey)) {
            FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(bussinessKey);
           FormViewResourceEntity formViewResourceEntity =  formViewResourceService.queryByViewFormId(bussinessKey);
           if (BeanUtil.isNotEmpty(formViewResourceEntity)){
               if (StringUtil.isNotEmpty(formViewResourceEntity.getResourceId())){
                    ucResourcesService.enabledResources(formViewResourceEntity.getResourceId());
               }else {
                   UcResourcesEntity ucResourcesEntity = new UcResourcesEntity();
                   ucResourcesEntity.setResName(formViewResourceEntity.getResourceName());
                   ucResourcesEntity.setShowName(formViewResourceEntity.getResourceName());
                   ucResourcesEntity.setUrl("masterData/List/"+formViewResourceEntity.getFormId());
                   ucResourcesEntity.setHostId(formViewResourceEntity.getHostId());
                   ucResourcesEntity.setSortNo(Double.parseDouble(formViewResourceEntity.getResourceSort()));
                   ucResourcesEntity.setResCode(formViewResourceEntity.getResourceCode());
                   ucResourcesEntity.setParentId(formViewResourceEntity.getParentId());
                   ucResourcesEntity.setCreateId(formViewResourceEntity.getCreateId());
                   ucResourcesEntity.setIsLast(formViewResourceEntity.getIsLast());
                   ucResourcesEntity.setResType(formViewResourceEntity.getResourceType());
                    UcResourcesEntity ucResources = ucResourcesService.savePublishResources(ucResourcesEntity);
                    formViewResourceEntity.setResourceId(ucResources.getId());
                    formViewResourceService.updateById(formViewResourceEntity);
               }
               formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUTON);
               formDefinitionService.updateById(formDefinitionEntity);
           }
        }
        return RestResponse.error();
    }

    @RequestMapping(value = "/refusePutOnApproval", method = RequestMethod.POST)
    @ApiOperation(value = "反对上架申请", notes = "反对上架申请")
    public RestResponse refusePutOnApproval(String bussinessKey) {
        if (StringUtil.isNotEmpty(bussinessKey)) {
            FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(bussinessKey);
                formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUBLISH);
                formDefinitionService.updateById(formDefinitionEntity);
                FormViewResourceEntity formViewResourceEntity = new FormViewResourceEntity();
                formViewResourceEntity.setFormId(bussinessKey);
                 FormViewResourceEntity  formViewResource = formViewResourceService.getOne(formViewResourceEntity);
                 formViewResource.setStatus("2");
                 formViewResourceService.updateById(formViewResource);
            }
        return RestResponse.error();
    }


    @RequestMapping(value = "/agreeDownApproval", method = RequestMethod.POST)
    @ApiOperation(value = "下架申请通过", notes = "下架申请通过")
    public RestResponse agreeDownApproval(String bussinessKey) {
        if (StringUtil.isNotEmpty(bussinessKey)) {
            FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(bussinessKey);
            FormViewResourceEntity formViewResourceEntity = formViewResourceService.queryByViewFormId(bussinessKey);
            if (BeanUtil.isNotEmpty(formViewResourceEntity)) {
                if (StringUtil.isNotEmpty(formViewResourceEntity.getResourceId())) {
                    ucResourcesService.unenabledResources(formViewResourceEntity.getResourceId());
                    formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUBLISH);
                    formDefinitionService.updateById(formDefinitionEntity);

                }
            }
        }
        return RestResponse.error();
    }

    @RequestMapping(value = "/refuseDownApproval", method = RequestMethod.POST)
    @ApiOperation(value = "反对下架申请", notes = "反对下架申请")
    public RestResponse refuseDownApproval(String bussinessKey) {
        if (StringUtil.isNotEmpty(bussinessKey)) {
            FormDefinitionEntity formDefinitionEntity = formDefinitionService.getById(bussinessKey);
            formDefinitionEntity.setPublishApprovalStatus(FormDefinitionEntity.BIZ_STATUS_PUTON);
            formDefinitionService.updateById(formDefinitionEntity);
            FormViewResourceEntity formViewResourceEntity = new FormViewResourceEntity();
            formViewResourceEntity.setFormId(bussinessKey);
            FormViewResourceEntity  formViewResource = formViewResourceService.getOne(formViewResourceEntity);
            formViewResource.setStatus("2");
            formViewResourceService.updateById(formViewResource);
        }
        return RestResponse.error();
    }



    @RequestMapping(value = "/getFormViewResByFormId", method = RequestMethod.GET)
    @ApiOperation(value = "上下架审批详情", notes = "上下架审批详情")
    public FormViewResourceEntity getFormViewResByFormId(String formId) {
        FormViewResourceEntity formViewResourceEntity = new FormViewResourceEntity();
        if (StringUtil.isNotEmpty(formId)) {
            formViewResourceEntity =  formViewResourceService.queryByViewFormId(formId);
            if(BeanUtil.isNotEmpty(formViewResourceEntity)){
                UcResourcesEntity ucResourcesEntity = ucResourcesService.queryById(formViewResourceEntity.getParentId());
                if (BeanUtil.isNotEmpty(ucResourcesEntity)) {
                    formViewResourceEntity.setManuName(ucResourcesEntity.getShowName());
                }
            }
            }
        return formViewResourceEntity;
    }
}